package lu.med.praxiswahl.wahlumed.client.arztdatabase;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;
import lu.med.praxiswahl.wahlumed.client.utilities.ColumnWidthControler;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;

/**
 * 
 * @author Lars Holger Roth
 */
@SuppressWarnings("serial")
public class ArztTable extends JTable {

	private ColumnWidthControler c;
	private boolean isColumnWidthChanged;
	private TableColumn resizedColumn;

	public ArztTable(ArztTableModel model) {
		super(model);
		this.setRowHeight(30);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.selectionModel = getSelectionModel();
		this.selectionModel
				.addListSelectionListener(new ArztTableSelectionListener(model));
		this.setDefaultRenderer(Object.class, new ArztCellRenderer(model));
		this.setDefaultEditor(Object.class, new ArztCellEditor(model));

		c = new ColumnWidthControler(Main.COLUMNWIDTH_CONFIGURATION_FILE,
				"arzttbl", this);

		TableColumn column = null;
		for (int i = 0; i <= 7; i++) {
			column = this.getColumnModel().getColumn(i);
			switch (i) {
			case 0:
				column.setPreferredWidth(c.getColumnWidth(0));
				break;
			case 1:
				column.setPreferredWidth(c.getColumnWidth(1));
				break;
			case 2:
				column.setPreferredWidth(c.getColumnWidth(2));
				break;
			case 3:
				column.setPreferredWidth(c.getColumnWidth(3));
				break;
			case 4:
				column.setPreferredWidth(c.getColumnWidth(4));
				break;
			case 5:
				column.setPreferredWidth(c.getColumnWidth(5));
				break;
			case 6:
				column.setPreferredWidth(c.getColumnWidth(6));
				break;
			case 7:
				column.setPreferredWidth(c.getColumnWidth(7));
				break;
			}
		}

		this.getColumnModel().addColumnModelListener(
				new TableColumnWidthListener(this));
		this.getTableHeader().addMouseListener(
				new TableHeaderMouseListener(this, c));

	}

	public boolean getColumnWidthChanged() {
		return isColumnWidthChanged;
	}

	public void setColumnWidthChanged(boolean widthChanged,
			TableColumn resizedColumn) {
		isColumnWidthChanged = widthChanged;
		this.resizedColumn = resizedColumn;
	}

	public TableColumn getResizedColumn() {
		return resizedColumn;
	}

}
